package com.fantasy.gencim;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Properties;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import com.fantasy.excel.ReadExcel;
import com.fantasy.file.GetFolderAbsPath;
import com.fantasy.file.WriteTxt;
import com.fantasy.readproperties.ReadProperties;



public class GenCim {
	
	public static void main(String[] args) {
		//System.out.println(getF("12/23-c8"));
		//System.out.println(getK("12/23-c8"));
		
		writeCim("5.7-PRC cny test.xls");
		
	}
	

	
public static void writeCim(String fileName) {
		
		File file = null;
		file = new File(GetFolderAbsPath.getPath() + "/" + fileName);
		System.out.println("========xls== " + fileName);
		InputStream rptXlsInput;
		Properties mainConfig = ReadProperties.read("mainConfig.properties");
		Properties dataConfig = ReadProperties.read("dataConfig.properties");
		Enumeration<?> dataConfigEnu = dataConfig.propertyNames();
		HashMap<String, String> changeValue = new HashMap<String , String>();
		
		try {
			rptXlsInput = new FileInputStream(file);

			POIFSFileSystem fs = new POIFSFileSystem(rptXlsInput);

			HSSFWorkbook wb = new HSSFWorkbook(fs);

			HSSFSheet sheet = wb.getSheetAt(0);

			WriteTxt.open(GetFolderAbsPath.getPath() + mainConfig.getProperty("outputName", "gencim.cim") );
			
			int dataInfo =  Integer.valueOf(mainConfig.getProperty("dataInfo")) -1;
			int dataName =  Integer.valueOf(mainConfig.getProperty("dataName")) -1;
			int dataStart =  Integer.valueOf(mainConfig.getProperty("dataStart")) -1;
			// 得到总行数
			int rowNum = sheet.getLastRowNum();
			HSSFRow dataInfoRow = sheet.getRow(dataInfo);
			HSSFRow dataNameRow = sheet.getRow(dataName);
			int colNum = dataInfoRow.getLastCellNum();
			
			//System.out.println("=============-------------------------==============");
			System.out.println("colTatol = " + dataInfoRow.getLastCellNum());
			HSSFRow dataRow = null;
			for (int row = dataStart; row <= rowNum; row++) {
				//i
				if(row == dataStart){
					WriteTxt.write("@@batchload " + getProgromName(sheet , mainConfig.getProperty("progrom")) + "\r\n");
				}else{
					WriteTxt.write("\r\n@@batchload " + getProgromName(sheet , mainConfig.getProperty("progrom")) + "\r\n");
				}
				dataRow = sheet.getRow(row);
				int allf = 0;
				int allk = 0;
				for(int col = 0 ; col < colNum ; col ++){
					HSSFCell dataNameCell = dataNameRow.getCell(col);
					String name = ReadExcel.getCellFormatValue(dataNameCell).trim();
					if (name.toUpperCase().contains("Value".toUpperCase())) {
						WriteTxt.write(changeValue.get(name));
						changeValue.remove(name);
					}else{
						HSSFCell dataCell = dataRow.getCell(col);
						HSSFCell infoCell = dataInfoRow.getCell(col);
						
						String data = ReadExcel.getCellFormatValue(dataCell).trim();
						String info = ReadExcel.getCellFormatValue(infoCell).trim();
						int f = getF(info);
						int k = getK(info);
						String format = getFormat(info);
						while (dataConfigEnu.hasMoreElements()) {
							String key = (String) dataConfigEnu.nextElement();
							if (key.contains(name)) {
								changeValue.put(key + "Value", dataConfig.getProperty(key + "Value"));
							}
						}
						
						int betweenf = f - allf - 1;
						if(betweenf >= 1){
							for(int i = 0 ; i < betweenf ; i++){
								WriteTxt.write("\r\n - \r\n");
							}
							allf = f;
							allk = 0;
						}else{
							if(col != 0 && allf != f){
								WriteTxt.write("\r\n");
								allk = 0;
							}
							allf = f;
						}
						int betweenk = k - allk - 1;
						if(betweenk >= 1){
							for(int i = 0 ; i < betweenk ; i++){
								WriteTxt.write(" - ");
							}
						}
						//System.out.println(data);
						if(allk == 0){
							WriteTxt.write(getData(data , format , false));
						}else{
							WriteTxt.write(getData(data , format , true));
						}
						allk = k;
					}
				}
				WriteTxt.write("\r\n@@end");
			}
			wb.close();
			WriteTxt.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
    public static String getFormat(String info){
    	String format = "";
		format = info.substring(info.indexOf("-") + 1);
		return format;	
    }

	public static String getData(String data , String format , boolean prefix){
		String ret = "";
		if(data.equals("-") || format.equals("N")){
			ret = " " + data;
		}else{
			if(prefix){
				ret = " \"" + data + "\"";
			}else{
				ret = "\"" + data + "\"";
			}
		}
		return ret;
	}

	public static String getProgromName(HSSFSheet sheet,String progromRaw){
		HSSFRow progromRow = sheet.getRow(Integer.valueOf(progromRaw) -1 );
		String progromName = ReadExcel.getCellFormatValue(progromRow.getCell(0)).trim();
		if(progromName.toUpperCase().contains(".P")){
		}else{
			progromName = progromName + ".p";
		}
		return progromName;
	}

	public static int getK(String s){
		String k = "";
		k = s.substring(s.indexOf("/") + 1 , s.indexOf("-"));
		return Integer.valueOf(k);	
	}

	public static int getF(String s){
		String f = "";
		f = s.substring(0,s.indexOf("/"));
		return Integer.valueOf(f);
	}
	

}
